import { ObjectId } from 'mongodb';
import BaseModel from './MongoBaseModel';

interface IMind {
  _id?: ObjectId;
  story_id?: string;
  story_name?: string;
  branch?: string;
  project_id?: number;
  project_name?: string;
  env?: any;
  [propName: string]: any;
}

class Model extends BaseModel<IMind> {
  constructor() {
    super('mind');
  }

  async updateMany(query, data) {
    const db = await this.init();
    return db.updateMany(query, {
      $set: data,
    });
  }

  async getListWithStory() {
    const db = await this.init();
    const ret = db.aggregate([
      {
        $lookup: {
          from: 'storys',
          localField: 'story_id',
          foreignField: 'id',
          as: 'defineField',
        },
      },
      {
        $unwind: {
          path: '$defineField',
          preserveNullAndEmptyArrays: true,
        },
      },
      {
        $project: {
          branch: 1,
          developer: 1,
          env: 1,
          parentid: 1,
          project_id: 1,
          project_name: 1,
          story_id: 1,
          _id: 1,
          story_name: '$defineField.name',
          isMergeBranch: 1,
        },
      },
    ]);
    return ret.toArray();
  }
}

export default new Model();
